package com.fjec.market.schedule.handler;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fjec.market.entity.InfoFarmer;
import com.fjec.market.entity.InfoPurchase;
import com.fjec.market.helper.MysqlShopNumHelper;
import com.fjec.market.service.IInfoFarmerService;
import com.fjec.market.service.IInfoPurchaseService;
import com.xxl.job.core.biz.model.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class FarmerShopNumHandler {

    @Resource
    private IInfoFarmerService farmerService;

    @Resource
    private MysqlShopNumHelper mysqlShopNumHelper;

    public int handle(int limit, int minutes) {
        int num = 0;
        List<Map<String, Object>> mapList = farmerService.getFarmerForShopNumGen(limit, minutes);
        for (Map<String, Object> map : mapList) {
            // 生成shopNum
            try {
                Long id = MapUtils.getLong(map, "Id");
                Integer markCode = MapUtils.getInteger(map, "MarkCode");
                if (id != null && markCode != null) {
                    Long genNum = mysqlShopNumHelper.genShopNum(markCode);
                    if (genNum != null) {
                        String strNum = String.format("%d%06d", markCode, genNum);
                        UpdateWrapper<InfoFarmer> updateWrapper = new UpdateWrapper<>();
                        updateWrapper.eq("Id", id);
                        InfoFarmer entity = new InfoFarmer();
                        entity.setShopNum(Long.valueOf(strNum));
                        farmerService.update(entity, updateWrapper);
                        num++;
                    }
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
        return num;
    }


}
